package com.payment.service.alast.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.payment.entity.alast.common.ServicePlanOrderRecord;
import com.payment.entity.alast.stripe.autorenewal.setupintent.request.RequestStripeAutoPay;
import com.payment.entity.alast.stripe.autorenewal.setupintent.request.SetupIntentRequest;
import com.payment.entity.alast.stripe.autorenewal.setupintent.response.SetupIntentResponse;
import com.payment.entity.alast.stripe.autorenewal.subcription.cancel.request.RequestStripeSubCancel;
import com.payment.entity.alast.stripe.autorenewal.subcription.cancel.request.SubscriptionCancelRequest;
import com.payment.entity.alast.stripe.autorenewal.subcription.cancel.response.SubscriptionCancelResponse;
import com.payment.entity.alast.stripe.autorenewal.subcription.directly.request.RequestStripeSubDirectly;
import com.payment.entity.alast.stripe.autorenewal.subcription.directly.request.SubscriptionDirectlyPlan;
import com.payment.entity.alast.stripe.autorenewal.subcription.directly.request.SubscriptionDirectlyProduct;
import com.payment.entity.alast.stripe.autorenewal.subcription.directly.request.SubscriptionDirectlyRequest;
import com.payment.entity.alast.stripe.autorenewal.subcription.directly.response.SubscriptionDirectlyResponse;
import com.payment.entity.alast.stripe.onetime.request.CreateStripePaymentRequest;
import com.payment.entity.alast.stripe.onetime.request.RequestStripePay;
import com.payment.entity.alast.stripe.onetime.response.CreateStripePaymentResponse;
import com.payment.utils.Constant;
import com.payment.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 对接北京收银台的业务处理实现类
 *
 * @author lm
 * @date 2021/1/14 14:01
 */
@Component
public class ToBeijingCheckoutService {
    private static Logger logger = Logger.getLogger(ToBeijingCheckoutService.class);
    /**
     * 去订购套餐的路径：分测试路径和正式路径，在配置文件这边一起管理
     */
    @Value("${stripe.url}")
    private String stripeUrl;
    /**
     * stripe收款的邮箱
     */
    @Value("${stripe.receiptEmail}")
    private String receiptEmail;

    /**
     * Stripe支付： 一次性付款请求收银台获取支付权限
     *
     * @param re
     * @return
     */
    public CreateStripePaymentResponse createStripePayment(RequestStripePay re) {
        CreateStripePaymentRequest request = new CreateStripePaymentRequest();
        request.setAmount(re.getPayAmount());
        request.setCurrency(re.getPayCurrency());
        request.setOrder_id(re.getPayId());
        request.setPartnerCode(re.getPartnerCode());
        request.setEmail(re.getEmail());
        request.setReceipt_email(receiptEmail);

        // 开始组装json
        String requestStr = JSON.toJSONString(request);
        logger.info("【Stripe一次性支付】请求：" + requestStr);
        String url = stripeUrl + Constant.PAYMENT_INTENT;
        // 发送post请求，并接收返回结果
        String returnStr = HttpUtil.httpPost(url, requestStr);
        logger.info("url=" + url);

        logger.info("【Stripe一次性支付】响应：" + returnStr);
        if (StringUtils.isBlank(returnStr)) {
            return null;
        }
        return JSONArray.parseObject(returnStr, CreateStripePaymentResponse.class);
    }

    /**
     * Stripe支付：自动续费请求收银台获取支付权限
     *
     * @param
     * @return
     */
    public SetupIntentResponse setupIntent(RequestStripeAutoPay re, String email) {
        SetupIntentRequest request = new SetupIntentRequest();
        request.setDescription(Constant.STRIPE_DESC);
        request.setPartnerCode(re.getPartnerCode());
        request.setEmail(email);
        request.setReceipt_email(receiptEmail);

        // 开始组装json
        String requestStr = JSON.toJSONString(request);
        logger.info("【Stripe自动续费获取权限】请求：" + requestStr);
        String url = stripeUrl + Constant.SETUP_INTENT;

        // 发送post请求，并接收返回结果
        String returnStr = HttpUtil.httpPost(url, requestStr);
        logger.info("url=" + url);

        logger.info("【Stripe自动续费获取权限】响应：" + returnStr);
        if (StringUtils.isBlank(returnStr)) {
            return null;
        }
        return JSONArray.parseObject(returnStr, SetupIntentResponse.class);
    }

    /**
     * Stripe支付：自动续费直接订阅请求收银台
     *
     * @param
     * @return
     */
    public SubscriptionDirectlyResponse directly(SubscriptionDirectlyRequest request) {

        // 开始组装json
        String requestStr = JSON.toJSONString(request);
        logger.info("【Stripe自动续费】请求：" + requestStr);
        String url = stripeUrl + Constant.SUBSCRIPTION_DIRECTLY;

        // 发送post请求，并接收返回结果
        String returnStr = HttpUtil.httpPost(url, requestStr);
        logger.info("url=" + url);

        logger.info("【Stripe自动续费】响应：" + returnStr);
        if (StringUtils.isBlank(returnStr)) {
            return null;
        }
        return JSONArray.parseObject(returnStr, SubscriptionDirectlyResponse.class);
    }

    /**
     * Stripe支付：自动续费取消订阅请求收银台
     *
     * @param re
     * @return
     */
    public SubscriptionCancelResponse cancel(RequestStripeSubCancel re) {
        SubscriptionCancelRequest request = new SubscriptionCancelRequest();
        request.setPartnerCode(re.getPartnerCode());
        request.setEmail(re.getEmail());
        request.setOrder_id(re.getPayId());
        request.setPlan_id(re.getPlan_id());
        request.setPm_id(re.getPm_id());
        request.setSub_id(re.getSub_id());

        // 开始组装json
        String requestStr = JSON.toJSONString(request);
        logger.info("【Stripe自动续费取消订阅】请求：" + requestStr);
        String url = stripeUrl + Constant.SUBSCRIPTION_CANCEL;

        // 发送post请求，并接收返回结果
        String returnStr = HttpUtil.httpPost(url, requestStr);
        logger.info("url=" + url);

        logger.info("【Stripe自动续费取消订阅】响应：" + returnStr);
        if (StringUtils.isBlank(returnStr)) {
            return null;
        }
        return JSONArray.parseObject(returnStr, SubscriptionCancelResponse.class);
    }
}
